import threading

import keyboard


class Mission:
    control = None

    mission_numb = 0
    control_switch = 1

    send_switch = False

    on_fly = True

    speed = 40
    roll = 0
    pitch = 0
    throttle = 0
    yaw = 0

    main_thread = None
    send_thread = None
    switch_thread = None

    def __init__(self, control=None):
        self.control = control

    def start(self):
        if self.main_thread is None:
            self.main_thread = threading.Thread(target=self.maintain)
        self.main_thread.start()

        if self.switch_thread is None:
            self.switch_thread = threading.Thread(target=self.switch)
        self.switch_thread.start()

        if self.send_thread is None:
            self.send_thread = threading.Thread(target=self.send)
        self.send_thread.start()

    def maintain(self):
        while True:
            self.control_deal()
            self.mission_deal()

    def switch(self):
        while True:
            if keyboard.is_pressed('p'):
                self.control_switch = 0
                if self.on_fly:
                    self.send_switch = True
                else:
                    self.send_switch = False
            if keyboard.is_pressed('o'):
                self.control_switch = 1
                self.send_switch = False

    def send(self):
        while True:
            if self.send_switch and self.on_fly and self.control_switch == 1:
                self.control.set_rc(self.roll, self.pitch, self.throttle, self.yaw)

    def control_deal(self):
        while self.control_switch == 1:
            # 发送手动控制信息
            if self.on_fly:
                if keyboard.is_pressed('w'):
                    self.pitch = -self.speed
                elif keyboard.is_pressed('s'):
                    self.pitch = self.speed
                else:
                    self.pitch = 0

                if keyboard.is_pressed('a'):
                    self.roll = -self.speed
                elif keyboard.is_pressed('d'):
                    self.roll = self.speed
                else:
                    self.roll = 0

                if keyboard.is_pressed('space'):
                    self.throttle = self.speed
                elif keyboard.is_pressed('shift'):
                    self.throttle = self.speed
                else:
                    self.throttle = 0

                if keyboard.is_pressed('left'):
                    self.yaw = self.speed
                elif keyboard.is_pressed('right'):
                    self.yaw = -self.speed
                else:
                    self.yaw = 0

                if keyboard.is_pressed('y'):
                    self.on_fly = False
                    self.control.land()
                    self.control.wait()

            else:
                if keyboard.is_pressed('t'):
                    self.control.takeoff()
                    self.control.wait()
                    self.on_fly = True

    def mission_deal(self):
        while self.control_switch == 0:
            if self.mission_numb == 0:
                pass
            elif self.mission_numb == 1:
                pass


if __name__ == '__main__':
    m = Mission()
    while True:
        print(6)
